﻿using KYJControlLib;
using KYJScadaProModbusRtu;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.ControlLib;

namespace KYJScadaProModbusRTU
{
    public partial class FrmMonitor : Form
    {
        public FrmMonitor()
        {
            InitializeComponent();

            //开启定时器
            updateTimer = new Timer();
            updateTimer.Interval = 200;
            updateTimer.Tick += UpdateTimer_Tick;
            updateTimer.Start();

            //FormClosing事件关闭定时器
            this.FormClosing += (sender, e) =>
            {
                this.updateTimer.Stop();
            };
        }

        //实时更新的定时器
        private Timer updateTimer;

        // 更新控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            UpdateCommon(this);
            if (CommomMethods.sysConfig.AutoLock)
            {
                CommomMethods.tickCount++;
                ;
                if (CommomMethods.tickCount == CommomMethods.sysConfig.LockPeriod * 60 * 1000 / this.updateTimer.Interval)
                {
                    //锁屏
                    LockWorkStation();
                }
            }
        }

        #region 通用更新方法
        private void UpdateCommon(Control parentControl)
        {
            if (CommomMethods.plcDevice != null && CommomMethods.connectState)//设备连接状态
            {
                foreach (Control item in parentControl.Controls)
                {
                    //更新TextShow显示
                    if (item is TextShow textShow)
                    {
                        if (textShow.BindVarName != null && textShow.BindVarName.Length > 0)
                        {
                            float value = 0.0f;
                            if (float.TryParse(CommomMethods.plcDevice[textShow.BindVarName]?.ToString(), out value))
                            {
                                textShow.CurrentValue = value.ToString("f1");
                            }
                        }
                    }
                    //更新冷却泵状态
                    else if (item is Pump pump)
                    {
                        if (pump.BindVarName != null && pump.BindVarName.Length > 0)
                        {
                            byte value = 0;
                            if (byte.TryParse(CommomMethods.plcDevice[pump.BindVarName]?.ToString(), out value))
                            {
                                pump.PumpState = value;
                            }
                        }
                    }
                    //更新阀门状态
                    else if (item is Tap tap)
                    {
                        if (tap.BindVarName != null && tap.BindVarName.Length > 0)
                        {
                            byte value = 0;
                            if (byte.TryParse(CommomMethods.plcDevice[tap.BindVarName]?.ToString(), out value))
                            {
                                tap.TapState = value;
                            }
                        }
                    }
                    else if (item.HasChildren)//递归
                    {
                        UpdateCommon(item);
                    }
                }
            }
        }
        #endregion

        private void pump1_PumpDoubleClick(object sender, EventArgs e)
        {
            if (sender is Pump pump)
            {
                new FrmPumpControl(pump.PumpIndex).ShowDialog();
            }
        }

        private void tap_Common_DoubleClick(object sender, EventArgs e)
        {
            if (sender is Tap tap)
            {
                new FrmTapControl().ShowDialog();
            }
        }

        #region 系统锁屏
        [DllImport("user32")]
        public static extern bool LockWorkStation();
        #endregion

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }
        #endregion
    }
    #region 消息筛选器
    internal class MessageFilter : IMessageFilter
    {
        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == 0x0200 || m.Msg == 0x0201 || m.Msg == 0x0204 || m.Msg == 0x0207)
            {
                CommomMethods.tickCount = 0;
            }
            return false;
        }
    }

    #endregion
}
